import { find } from "lodash";
import { Modal } from "antd";

//添加商品展示排序
const sortGoodsByGoodsType = list => {
	//0包装 1赠品 2正常商品
	const packages = [],
		gifts = [],
		goods = [];

  list.map(item=>{
    const { orderGoodsType } = item;
    if(orderGoodsType == 'package'){
      packages.push(item);
    }else if(orderGoodsType == 'gifts'){
      gifts.push(item);
    }else{
      goods.push(item);
    }
  })
  return [...goods, ...gifts, ...packages];
}

//处理自发跑量
const formatZifaRunGoods = (item) => {
	const { skuCode, id, goodsNum = 1, image, skuName, purchasePrice, colorSpecifications, weight, spuCode, availableInventory } = item;
	const  goodsPrice = purchasePrice || 0;
	return {
		...item,
		type: 2,//商品
		orderGoodsType: 'runGoods',
		goodsCode: skuCode,
		goodsImage: image, 
		goodsName: skuName,
		goodsPrice,
		goodsNum: goodsNum || 1,
		amount: goodsPrice * 1000 * goodsNum / 1000,
		goodsSpec: colorSpecifications,
		goodsWeight: weight,
		isCustom: 0,
		isVipGoods: 0,
		outStoreSku: skuCode,
		outStoreSpu: spuCode,
		storeName: '/',
		goodsId: id,
		skuId: id,
		sysGoodsId: id,
		availableInventory: availableInventory || '0'
	}
}

//处理代发跑量
const formatDaifaRunGoods = (item) => {
  const { goodsId, goodsCommonId, goodsPrice, vipPrice, goodsNum } = item, goodsPriceValue = vipPrice || goodsPrice || 0,
	goodsNumValue = goodsNum || 1;
	return {
		...item,
		type: goodsId == '7720669' ? 0 : 2,
		orderGoodsType: goodsId == '7720669' ? 'runPackage' : 'runGoods',
		goodsPrice: goodsPriceValue,
		goodsNum: goodsNumValue,
		amount: goodsPriceValue * 1000 * goodsNumValue / 1000,
		isVipGoods: Boolean(vipPrice) ? 1 : 0,
		outStoreSku: goodsId,
		outStoreSpu: goodsCommonId,
	}
}

//处理添加商品
const formatGoodsInfo = (item) => {
	const { isMatch, skuCode, id, num, image, skuName, memberPrice, colorSpecifications, weight, spuCode, goodsSkuMatches = [], combinationSubList = [] } = item;
	const { skuId, supplierId, supplierName, supplierSpuCode, matchInfos = {} } = find(goodsSkuMatches, ['isDefault', 1]) || {};//匹配供应商信息
	const goodsPrice =  item.purchasePrice || 0, goodsNum = num || 1;
	return ({
		...item,
		orderGoodsType: 'goods',
		goodsCode: skuCode,
		goodsImage: image, 
		goodsName: skuName,
		goodsNum,
		goodsPrice,
		amount: goodsPrice * 1000 * goodsNum / 1000,
		goodsSpec: colorSpecifications,
		goodsWeight: weight,
		isCustom: 0,
		isVipGoods: (isMatch == 1 && matchInfos?.matchType == 1 && Boolean(memberPrice)) ? 1 : 0,
		outStoreSku: skuCode,
		outStoreSpu: spuCode,
		spu: supplierSpuCode,
		storeId: supplierId,
		storeName: supplierName || '/',
		goodsId: skuId,
		skuId: id,
		sysGoodsId: id,
		type: 2,//0包装 1赠品 2正常商品 
		storeCode: item.shopCode,
		sysGoodsAbbreviation: item.skuAbbreviation,
	})
}

//处理添加商品
const formatCustomGoodsInfo = (item) => {
	const { color, storeId, goodsImage, goodsName, goodsNum, goodsPrice, storeName, shopCode, size } = item;
	return ({
		orderGoodsType: 'goods',
		goodsImage: goodsImage, 
		goodsName: goodsName,
		goodsNum: goodsNum,
		goodsPrice: goodsPrice,
		amount: goodsPrice * 1000 * goodsNum / 1000,
		goodsSpec: size + color,
		specs: size + color,
		storeId: storeId,
		storeName: storeName || '/',
		type: 3,//自定义商品
		storeCode: shopCode,
        spuCode: '',
        skuCode: ''
	})
}

//处理组合商品
const formatCombineGoodsInfo = (obj, inputNum = 1) => {
  // console.log(obj, 'obj')
	return obj.subList.map(item=>{
		const { isMatch, skuCode, id, num = 1, image, skuName, purchasePrice, spuId, colorSpecifications, weight, spuCode, matchInfos = {}, memberPrice } = item;
		const { matchType, supplierId, supplierName, supplierSkuId } = matchInfos;
	  const goodsPrice = purchasePrice || 0, goodsNum =  inputNum * num;
		const tempObj = {
			...item,
		  orderGoodsType: 'goods',
			sysGoodsName: skuName,
			sysGoodsSpec: colorSpecifications,
			sysGoodsImage: image,
			sysGoodsId: id,//子商品级别
			sysSpuCode: spuCode,
			sysSkuCode: skuCode,
			sysGoodsPrice: purchasePrice,
			sysSpuId: spuId,
			isCustom: (isMatch == 1 && matchType == 1) ? 1 : 0,
	  	isVipGoods: (isMatch == 1 && matchType == 1 && Boolean(memberPrice)) ? 1 : 0,
			goodsPrice,
			goodsNum,
			composeNum: num,
			// goodsName: skuName,
			// amount: goodsPrice * 1000 * goodsNum / 1000,
			sysComposeGoodsId: obj.id,
			isSysComposeGoods: 1,
			storeId: supplierId,
			storeName: supplierName || '/',
			goodsId: supplierSkuId,
      isMatch,
			type: 2,//0包装 1赠品 2正常商品 
		  storeCode: item.matchInfos?.shopCode,
			sysGoodsAbbreviation: item.skuAbbreviation,
		}

		const { sysGoodsName, sysGoodsSpec, sysGoodsPrice, sysGoodsImage, sysSpuCode, sysSkuCode } = tempObj;
		return ({
			...tempObj,
			goodsName: sysGoodsName,
			goodsSpec: sysGoodsSpec,
			// goodsPrice: sysGoodsPrice,
			goodsImage: sysGoodsImage ,
			spuCode: sysSpuCode,
			skuCode: sysSkuCode,
			amount: goodsPrice * 1000 * goodsNum / 1000,
		})

		// if(isMatch){

		// 	if(matchType == 1){
			
		// 	}else{
		// 		const { supplierSkuName, supplierSkuSpec, supplierSkuPrice, supplierSkuImage, supplierSpuCode, supplierSkuCode } = matchInfos;
		// 		return ({
		// 			...tempObj,
		// 			goodsName: supplierSkuName,
		// 			goodsSpec: supplierSkuSpec,
		// 			// goodsPrice: supplierSkuPrice,
		// 			goodsImage: supplierSkuImage ,
		// 			spuCode: supplierSpuCode,
		// 			skuCode: supplierSkuCode,
		// 	    amount: goodsPrice * 1000 * goodsNum / 1000,
		// 		})
		// 	}
		// }
		return tempObj;
	
	})
}

//根据勾选查询所有行数据
const getSelectedRowData = (selectKeys, pageData, rowKey = "id") => {
	return selectKeys
		.map(item => {
			return find(pageData, [rowKey, item]);
		})
		.filter(item => item);
};

//过滤订单状态
const filterOrderByState = (selectKeys, states) => {
	return getSelectedRowData(selectKeys).filter(item => {
		states.includes(item.state);
	});
};

//过滤符合条件的订单
const filterOrderByRules = otherRulesFunc => {
	// const { selectKeys, otherRulesFunc } = obj || {};
	return getSelectedRowData(selectKeys).filter(item => {
		otherRulesFunc(item);
	});
};

//公共过滤订单方法
const commonFilterOrder = obj => {
	const { sourceData, states, otherRulesFunc } = obj || {};

	const iteratorFunc = child => {
		return (!states?.length || states.includes(child.state)) && (typeof otherRulesFunc !== "function" || otherRulesFunc(child));
	};
	return sourceData.filter(item => iteratorFunc(item));
};

//订单状态映射
const orderStateMap = {
	10: "待审核",
	20: "待发货",
	30: "异常订单",
	40: "已发货",
	50: "已作废"
};

//根据订单状态分割数据（展示数量）
const statisticsNumbers = (states, orderList) => {
	const numObj = { total: 0 };
	Object.keys(orderStateMap).map(item => {
		numObj[item] = 0;
	});
	if (states?.length && orderList?.length) {
		orderList.map(item => {
			numObj[item.state] += 1;
		});
	}
	if (states?.length) {
		states.map(item => {
			numObj.total += numObj[item];
		});
	}
	return numObj;
};

//浓缩符合条件的id（接口入参）
const commonReduceId = (list, rowKey = "id") => {
	return list.reduce((prev, next) => {
		return prev.concat(next[rowKey]);
	}, []);
};

//确认提示
const commonModalConfirm = (title, content, callback) => {
	Modal.confirm({
		centered: true,
		title,
		content,
		onOk() {
			callback && callback();
		}
	});
};

// commonModalConfirm('审核提示', '只有待审核的订单能操作审核，其他订单已跳过！');
// commonModalConfirm('返审核提示', '只有待发货的订单能返审核，其他订单已跳过！');
// commonModalConfirm('修改发货仓提示', <><p>部分已支付的代发订单，不支持修改仓库，已跳过！</p>如有需要，请转正常单后再操作！</>);
// commonModalConfirm('设跑量提示', '部分异常已支付的代发订单，不能设跑量，已跳过！');
// commonModalConfirm('取消跑量提示', '部分异常已支付的代发订单，不能取消跑量，已跳过！');
// commonModalConfirm('设包装或赠品提示', '部分异常已支付的代发订单，不能设包装或赠品，已跳过！');
// commonModalConfirm('取消运费拦截', <><p>取消运费拦截的订单，仓库发货将不再考虑快递差价问题。</p>是否取消运费拦截？</>);

export default {
	sortGoodsByGoodsType,
	formatZifaRunGoods,
	formatDaifaRunGoods,
	formatGoodsInfo,
    formatCustomGoodsInfo,
	formatCombineGoodsInfo,
	getSelectedRowData,
	filterOrderByState,
	filterOrderByRules,
	commonFilterOrder,
	statisticsNumbers,
	commonReduceId
};
